<template>
  <div>
    <el-button
      class="addquestion"
      type="primary"
      size="small"
      @click="drawer = true"
      >新增试题</el-button
    >
    <!--  -->
    <el-table :data="questionsList" border style="width: 100%">
      <el-table-column type="index" label="#" width="60"> </el-table-column>
      <el-table-column prop="title" label="题目"> </el-table-column>
      <el-table-column prop="name" label="分类"> </el-table-column>
      <el-table-column prop="address" label="作者"> </el-table-column>
      <el-table-column prop="address" label="发布时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini">编辑</el-button>
          <el-button size="mini" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--  -->
    <el-drawer
      size="40%"
      title="我是标题"
      :visible.sync="drawer"
      :with-header="false"
    >
      <div style="padding: 20px">
        <el-page-header title="关闭" @back="drawer = false"> </el-page-header>
      </div>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="题目">
          <el-input
            type="textarea"
            v-model="form.title"
            size="small"
            :rows="1"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类">
          <el-select v-model="form.cid" placeholder="请选择分类">
            <el-option
              v-for="item in classifyList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选项">
          <el-checkbox-group v-model="form.answers">
            <div
              class="el-checkbox-row"
              v-for="(item, index) in optionsList"
              :key="index"
            >
              <el-checkbox :label="item.text" name="type"></el-checkbox>
              <el-input
                type="textarea"
                v-model="item.value"
                size="small"
                :rows="1"
              ></el-input>
              <template v-if="index >= 3">
                <el-button
                  type="success"
                  icon="el-icon-plus"
                  circle
                  style="margin-left: 10px; margin-top: 5px"
                  @click="addOptions"
                  v-if="
                    index == optionsList.length - 1 && optionsList.length < 7
                  "
                ></el-button>
                <el-button
                  type="danger"
                  icon="el-icon-minus"
                  style="margin-left: 10px; margin-top: 5px"
                  circle
                  v-if="index >= 4 && optionsList.length > 4"
                  @click="deleteOptions(item.value)"
                ></el-button>
              </template>
            </div>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="答案">
          <div>{{ sortAnswers.join() }}</div>
        </el-form-item>
        <el-form-item label="答案提示">
          <el-input type="textarea" v-model="form.tips" :rows="2"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">提交</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
    <!--  -->
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
export default {
  layout: "msbd",
  data() {
    return {
      drawer: false,
      questionsList: [{ id: 1 }],
      form: {
        answers: [],
      },
      optionsAm: [65, 66, 67, 68],
      optionsList: [
        { text: "A", value: "" },
        { text: "B", value: "" },
        { text: "C", value: "" },
        { text: "D", value: "" },
      ],
    };
  },
  mounted() {
    this.getClassify();
  },
  methods: {
    ...mapActions("classify", ["getClassify"]),
    // 新增选项
    addOptions($event) {
      this.optionsAm.push((this.optionsAm[this.optionsAm.length - 1] += 1));
      if (this.optionsList.length < 7) {
        this.optionsList.push({
          text: String.fromCharCode(this.optionsAm[this.optionsAm.length - 1]),
          value: "",
        });
      }
    },
    // 删除选项
    deleteOptions(val) {
      if (
        this.form.answers[this.form.answers.length - 1] ==
        this.optionsList[this.optionsList.length - 1].text
      ) {
        this.form.answers.splice(this.form.answers.length - 1, 1);
      }
      this.optionsList.splice(this.optionsList.length - 1, 1);
      this.optionsList[this.optionsList.length - 1].value = val;
    },
    // 提交
    onSubmit() {
      this.form.options = this.optionsList;
      this.form.answers = this.sortAnswers;
      console.log(this.form);
    },
  },
  computed: {
    ...mapGetters(["classifyList"]),
    sortAnswers() {
      return this.form.answers.sort();
    },
  },
};
</script>

<style scoped>
.addquestion {
  margin: 5px 0px 10px 5px;
}

.el-checkbox-row {
  display: flex;
}

.el-checkbox-row .el-textarea {
  width: 75%;
  margin-top: 7px;
  margin-left: 10px;
}
</style>